package com.me.batm.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.g2d.tiled.TileAtlas;
import com.badlogic.gdx.graphics.g2d.tiled.TiledLoader;
import com.badlogic.gdx.graphics.g2d.tiled.TiledMap;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.me.batm.GameConstants;
import com.me.batm.OurGdxGame;
import com.me.batm.input.GameInputListener;
import com.me.batm.input.InputStatus;
import com.me.batm.models.GameWorld;
import com.me.batm.services.GameConfiguration;
import com.me.batm.views.GameRenderer;
import com.me.batm.views.GameSoundService;

public class MainScreen implements Screen {
	private final String TAG = MainScreen.class.getSimpleName();

	OurGdxGame ourGdxGame;
	InputStatus inputStatus;
	GameWorld gameWorld;
	GameRenderer gameRenderer;
	Stage stage;
	String playerName;

	private GameSoundService gameSoundService;
	private boolean stopRender;

	public MainScreen(OurGdxGame ourGdxGame, String playerName){
		this.ourGdxGame = ourGdxGame;
		stage = new Stage();
		this.playerName = playerName;

		stopRender = false;
	}
	
	@Override
	public void render(float delta) {
		if(inputStatus.isSpace)
			delta *= GameConstants.GAME_SLOW_CONSTANT;		

		if(inputStatus.isMenu){
			this.dispose();
			GameConfiguration.setInitialValues();
			ourGdxGame.setScreen(new MenuScreen(ourGdxGame));
		}
		gameWorld.update(delta);
		if(gameWorld.player.isAlive == false) {
			this.dispose();
			ourGdxGame.setScreen(new SplashScreen(ourGdxGame));
		}
		
		if(gameWorld.target.isConquered()) {
			this.dispose();
			ourGdxGame.setScreen(nextLevel());
		}
		
		if(stopRender)
			return;
		
		gameSoundService.update(delta);
		gameRenderer.render();
		stage.act(Gdx.graphics.getDeltaTime());
		stage.draw();
        Table.drawDebug(stage);
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void show() {
		gameSoundService = new GameSoundService();
		
		int level = GameConfiguration.getActualLevel();
		Gdx.app.log(TAG, String.format("Starting Level = %d",level));

		// load the map
		//TiledMap tiledMap = TiledLoader.createMap(Gdx.files.internal("maps/level1/level1.tmx"));
		TiledMap tiledMap = TiledLoader.createMap(Gdx.files.internal(String.format("maps/level%d/level%d.tmx",level+1,level+1)));
		//TileAtlas tileAtlas = new TileAtlas(tiledMap, Gdx.files.internal("maps/level1"));		
		TileAtlas tileAtlas = new TileAtlas(tiledMap, Gdx.files.internal(String.format("maps/level%d",level+1)));
								
		inputStatus = new InputStatus();
		float timeOfLevel = GameConfiguration.getLevelTime(level);
		gameWorld = new GameWorld(inputStatus, tiledMap, tileAtlas, timeOfLevel);
		gameRenderer = new GameRenderer(gameWorld);

		gameSoundService.setGameWorld(gameWorld);
		gameSoundService.playGameMusic(0);

		GameInputListener gameInputListener = new GameInputListener(inputStatus);
		InputMultiplexer multiplexer = new InputMultiplexer(gameInputListener,new GestureDetector(gameInputListener));
		
		Gdx.input.setInputProcessor(multiplexer);
		//Gdx.input.setInputProcessor(stage);
		
		//Build Button
		createUI();
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
		Gdx.app.log(TAG, "Disposing");
		stopRender = true;
		gameWorld.dispose();
		gameRenderer.dispose();
		gameSoundService.dispose();
	}

	public void createUI() {
		//Menu Main Screen
		Skin skin = new Skin(Gdx.files.internal("data/uiskin.json"));
		Table table = new Table();		
		
		float width  = GameConstants.DESKTOP_SCREEN_WIDTH / 2.0f;
		float height = 15f;
		table.setSize(width, height);
		table.setPosition(GameConstants.DESKTOP_SCREEN_HEIGHT*0.09f ,
					      GameConstants.DESKTOP_SCREEN_HEIGHT - GameConstants.DESKTOP_SCREEN_HEIGHT * 0.2f);
		
		/*Label labelUser = new Label("PLAYER: " + this.playerName, skin);		
		table.add(labelUser).width(200);
		table.row();		
		table.left().top();
		stage.addActor(table);*/		
	}
	
	private Screen nextLevel(){
		int level = GameConfiguration.getActualLevel();
		String difficulty = GameConfiguration.getDifficulty();
		
		level++;
		if(level > GameConfiguration.getNumberOfMaps()){
			if(difficulty.equalsIgnoreCase(GameConfiguration.DIFFICULTY_NORMAL)){
				level = 1;
				difficulty = GameConfiguration.DIFFICULTY_HARD;
			}else{
				//win :)
				return new WinScreen(ourGdxGame,playerName);
			}
		}
		Gdx.app.log(TAG, String.format("Changing to level = %d[%s]",level,difficulty));
		
		gameRenderer.printStartLevelScreen(level, difficulty);
		
		GameConfiguration.setActualLevel(level);
		GameConfiguration.setDifficulty(difficulty);
		return new MainScreen(ourGdxGame,playerName);
	}

}
